package top.ztf.erp.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import top.ztf.erp.domain.User;
import top.ztf.erp.service.UserService;
import top.ztf.erp.service.imp.UserServiceImp;
import top.ztf.erp.utils.MD5Utils;
import top.ztf.erp.utils.ResponseClient;
import top.ztf.erp.vo.PermissionVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/user")
public class UserServlet extends BaseServlet{


    public void getUserAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher("WEB-INF/qx/user/add.jsp").forward(req,resp);
    }

    public void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InvocationTargetException, IllegalAccessException {
        UserService service = new UserServiceImp();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,parameterMap);
        int count = service.addUser(user);
        ResponseClient.common(count,response);
    }

    public void getUserUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserService service = new UserServiceImp();
        String id = request.getParameter("id");
        User userById = service.getUserById(id);
        request.setAttribute("user",userById);
        request.getRequestDispatcher("WEB-INF/qx/user/edit.jsp").forward(request,response);
    }

    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InvocationTargetException, IllegalAccessException {
        UserService service = new UserServiceImp();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        BeanUtils.populate(user,parameterMap);
        int count = service.updateUser(user);
        ResponseClient.common(count,response);
    }

    public void getUserAssign(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");

        UserService service = new UserServiceImp();
        Map<String, Object> assign = service.getAssignById(id);
        request.setAttribute("assign",assign);
        request.setAttribute("userId",id);
        request.getRequestDispatcher("WEB-INF/qx/user/assign.jsp").forward(request,response);
    }

    public void getUserDetail(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserService service = new UserServiceImp();
        String id = request.getParameter("id");
        User user= service.getUserById(id);
        request.setAttribute("user",user);
        request.getRequestDispatcher("WEB-INF/qx/user/detail.jsp").forward(request,response);
    }

    public void searchUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String searchName = request.getParameter("search_name");
        String startTime = request.getParameter("start_time");
        String endTime = request.getParameter("end_time");
        String lock = request.getParameter("lock");

        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        UserService service = new UserServiceImp();
        PageInfo<User> pageInfo = service.searchList(searchName,startTime,endTime,lock,pageNo,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("searchName",searchName);
        request.setAttribute("startTime",startTime);
        request.setAttribute("endTime",endTime);
        request.setAttribute("lock",lock);


        request.getRequestDispatcher("/WEB-INF/qx/user/list.jsp").forward(request,response);
    }

    public void saveAssign(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserService service = new UserServiceImp();
        String userId = request.getParameter("id");
        String[] parameterValues = request.getParameterValues("array[]");
        int count = service.updateAssign(userId,parameterValues);
        ResponseClient.common(count,response);
    }

    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserService service = new UserServiceImp();
        String[] parameterValues = request.getParameterValues("array[]");
        int count = service.deleteUser(parameterValues);
        ResponseClient.common(count,response);
    }
    public void gerPermissionByUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String id = request.getParameter("id");
        UserService userService  = new UserServiceImp();
        List<PermissionVo> permissionVoList = userService.getPermissionByUser(id);

        response.getWriter().print(JSONObject.toJSONString(permissionVoList));
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (request.getSession().getAttribute("user")!=null){
            ResponseClient.fail("你已经登录了,请不要重复登录",response);
            return;
        }

        String account = request.getParameter("account");
        String password = request.getParameter("password");
        UserService userService  = new UserServiceImp();
        User user = null;
        try {
            user = userService.login(account, MD5Utils.getMD5Str(password),"");
        }catch (Exception e){
            ResponseClient.fail(e.getMessage(),response);
            return;
        }
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        ResponseClient.success(response);
    }

    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        session.invalidate();
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }

    public void main(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (request.getSession().getAttribute("user")==null){
            response.sendRedirect(request.getContextPath()+"/login.html");
            return;
        }
        UserService service = new UserServiceImp();
        User user = (User) request.getSession().getAttribute("user");
        Set<String> stringSer =  service.getPermissionByButton(user);
        request.getSession().setAttribute("permissionAll",stringSer);
        request.getRequestDispatcher("WEB-INF/main.jsp").forward(request,response);
    }

    public void getPermissionByMenu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");
        UserService userService  = new UserServiceImp();
        List<Map<String,String>> mapList = userService.getPermissionByMenu(user);
        response.getWriter().print(JSONObject.toJSONString(mapList));
    }
}
